package com.ncloud.timetask.dashboard.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.timetask.dashboard.mapper.DashboardAdminTaskMapper;
import com.ncloud.timetask.dashboard.service.DashboardAdminTaskService;

/**
 * @author sundelong
 * project_name: timetask 
 * type_name: DasdboardAdminTaskService 
 * date: 2017年6月21日下午5:48:27 
 * 
 */
@Service
public class DasdboardAdminTaskServiceImpl implements DashboardAdminTaskService {
	@Autowired
	DashboardAdminTaskMapper adminMapper;
	
	@Override
	public String getSumBalanceByThisMonth(String taskId) {
		String sum = null;
		try {				
			sum = adminMapper.getSumBalanceByThisMonth();
			String year = ThisMonth().get("year");
			String month = ThisMonth().get("month");
			String taskkey = "sum";
			String remark = null;
			String taskValue2=null;
			String taskValue3=null;
			//判断这个月份的数据有没有插入表中，如果已插入就更新数据，没有就插入；
			int count = adminMapper.getExistence2(taskId, year, month,taskkey);
			if(count == 0){
				adminMapper.insertBsMonthlyTask(taskId, year, month, taskkey, sum,remark,taskValue2,taskValue3);
			}else{
				adminMapper.updateBsMonthlyTask2(sum, taskId, year, month,taskkey);
			}						
		} catch (Exception e) {
			e.printStackTrace();
			throw new UserException(ResultEnum.DATABASE_ERROR);
		}
		return null;
	}
	
	@Override
	public List<Map<Object, Object>> getSumBalanceByChnlAndThisMonth(String taskId) {
		List<Map<Object,Object>> listInfo = new ArrayList<Map<Object,Object>>();
		try {				
			String year = ThisMonth().get("year");
			String month = ThisMonth().get("month");
			listInfo = adminMapper.getSumBalanceByChnlAndThisMonth();
			for(int i = 0 ; i<listInfo.size();i++ ){
				Map<Object,Object> map = new HashMap<Object, Object>();
				map = listInfo.get(i);
				boolean flag=map.containsKey("chnl_name");
				String taskKey = null;
				if(flag){
					taskKey=map.get("chnl_name").toString();
					}

				boolean flag1=map.containsKey("sum");
				String taskValue = null;
				if(flag1){
					taskValue=map.get("sum").toString();
					}
				boolean flag2=map.containsKey("chnl_id");
				String chnlId = null;
				if(flag2){
					chnlId=map.get("chnl_id").toString();
					}		
				String remark = null;
				String taskValue2=chnlId;
				String taskValue3=null;
				
				//判断这个月份的数据有没有插入表中，如果已插入就更新数据，没有就插入；
				int count = adminMapper.getExistence3(taskId, year, month,taskKey,taskValue2);
				if(count == 0){
					adminMapper.insertBsMonthlyTask(taskId, year, month, taskKey, taskValue,remark,taskValue2,taskValue3);
				}else{
					adminMapper.updateBsMonthlyTask(taskValue, taskId, year, month,taskKey,taskValue2);
				}				
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new UserException(ResultEnum.DATABASE_ERROR);
		}
		return null;
	}

	@Override
	public void insertYearProductCount(String taskId1,String taskId2){		
		List<Map<Object,Object>> listInfo = new ArrayList<Map<Object,Object>>();
		try {				
			String year = ThisMonth().get("year");
			String month = ThisMonth().get("month");
			String remark = null;
			String taskValue3 = null;
			//套餐的销售金额
			listInfo = adminMapper.getCountDiscnt();
			for(int i = 0 ; i<listInfo.size();i++ ){
				Map<Object,Object> map = new HashMap<Object, Object>();
				map = listInfo.get(i);
				
				boolean flag0=map.containsKey("discnt_name");
				String taskKey = null;
				if(flag0){
					taskKey=map.get("discnt_name").toString();
					}
				if(taskKey !=null){
				boolean flag=map.containsKey("sum");
				String taskValue = null;
				if(flag){
					taskValue=map.get("sum").toString();
					}
				
				boolean flag1=map.containsKey("prod_name");
				String taskValue2 = null;
				if(flag1){
					taskValue2=map.get("prod_name").toString();
					}
				//判断这个月份的数据有没有插入表中，如果已插入就更新数据，没有就插入；
				int count = adminMapper.getExistence(taskId2, year, month,taskKey,taskValue2);
				if(count == 0){
					adminMapper.insertBsMonthlyTask(taskId2, year, month, taskKey, taskValue,remark,taskValue2,taskValue3);
				}else{
					adminMapper.updateBsMonthlyTask(taskValue, taskId2, year, month,taskKey,taskValue2);
				}
				}
			}
			//产品的销售金额
			listInfo = adminMapper.getCountByProduct();
			for(int i = 0 ; i<listInfo.size();i++ ){
				Map<Object,Object> map = new HashMap<Object, Object>();
				map = listInfo.get(i);
				String taskKey = map.get("prod_name").toString();
				boolean flag1=map.containsKey("fee");
				String taskValue = null;
				if(flag1){
					taskValue=map.get("fee").toString();
					}
				
				String taskValue2 = null;
				//判断这个月份的数据有没有插入表中，如果已插入就更新数据，没有就插入；
				int count = adminMapper.getExistence2(taskId1, year, month,taskKey);
				if(count == 0){
					adminMapper.insertBsMonthlyTask(taskId1, year, month, taskKey, taskValue,remark,taskValue2,taskValue3);
				}else{
					adminMapper.updateBsMonthlyTask2(taskValue, taskId1, year, month,taskKey);
				}				
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new UserException(ResultEnum.DATABASE_ERROR);
		}
	}	
	
	@Override
	public void insertTableByDay(String taskId){
		List<Map<Object,Object>> listInfo = new ArrayList<Map<Object,Object>>();
		try {							
			String year = ThisMonth().get("year");
			String month = ThisMonth().get("month");
			listInfo = adminMapper.getSettlementByProduct();
			String taskKey = null;
			String taskValue = null;
			String remark = null;
			String taskValue2=null;
			String taskValue3=null;
			for(int i=0;i<listInfo.size();i++){
				Map<Object,Object> map = new HashMap<Object, Object>();
				map = listInfo.get(i);
					boolean flag1=map.containsKey("prod_name");								
					if(flag1){
						taskKey=map.get("prod_name").toString();
								}
						 boolean flag=map.containsKey("fee");
							if(flag){
								taskValue=map.get("fee").toString();
						}
							boolean flag2=map.containsKey("prod_type");
							if(flag2){
								taskValue2=map.get("prod_type").toString();
						}
				//判断这个月份的数据有没有插入表中，如果已插入就更新数据，没有就插入；
				int count = adminMapper.getExistence(taskId, year, month,taskKey,taskValue2);
				if(count == 0){
					adminMapper.insertBsMonthlyTask(taskId, year, month, taskKey, taskValue,remark,taskValue2,taskValue3);
				}else{
					adminMapper.updateBsMonthlyTask2(taskValue, taskId, year, month,taskKey);
				}
			}
											
		} catch (Exception e) {
			e.printStackTrace();
			throw new UserException(ResultEnum.DATABASE_ERROR);
		}
	}	
	
	public Map<String,String> LastMonth(){
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, -1);
		SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM");
		String time = format.format(cal.getTime());
		String year = time.split("-")[0];
		String day = time.split("-")[1];
		Map<String,String> dateMap = new HashMap<String, String>();
		dateMap.put("year", year);
		dateMap.put("month",day);
		return dateMap;		
	}
	public Map<String, String> ThisMonth(){
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		String time = sdf.format(date);
		String year = time.split("-")[0];
		String day = time.split("-")[1];
		Map<String,String> dateMap = new HashMap<String, String>();
		dateMap.put("year", year);
		dateMap.put("month",day);
		return dateMap;		
	}
	
	
}
